/**
This file is part of dbdjasperreport.

Copyright (C) 2008 Kue Vang aka Kou
Licensed under the Eclipse Public License, Version 1.0; 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html 
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and limitations 
under the License.
**/

import java.io.Serializable
import java.text.SimpleDateFormat
import java.text.DateFormat

class ValidationService implements Serializable {
	def boolean isNumeric(String strToCheck){
	    def r=["0","1","2","3","4","5","6","7","8","9"]
		boolean valid = true
		//null value is consider valid
		if(strToCheck){
			for(int i=0;i<= strToCheck.size()-1;i++){
				if(!r.contains(strToCheck[i])){
					valid= false
					break
				}
			}
		}
		return valid
	}
	
	def boolean validDate(String dateStr)throws Exception{
		boolean valid =true
		try{			
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy")
			//check to see it the entered date is a valid date
			Date tempDate = df.parse(dateStr)
			if(df.format(tempDate)!=dateStr)
				valid = false	
			valid=continueCheck
		}
		catch(Exception e){
			return valid
		}
		return valid		
	}
	
	def boolean validDateFormat(String dateStr){
		boolean valid = true
		boolean continueCheck=true
		def r=["0","1","2","3","4","5","6","7","8","9"]
		def slash="/"
		if(dateStr.size()==10){
			def m=dateStr.substring(0,2)
			def slash1=dateStr.substring(2,3)
			def d=dateStr.substring(3,5)
			def slash2=dateStr.substring(5,6)
			def y=dateStr.substring(6)
			for(int i=0;i<=m.size()-1;i++){
				if(!r.contains(m[i])){
					valid = false
					continueCheck=false
					break
				}
			}
			if(continueCheck){
				if(slash!=slash1 | slash!=slash2){
					valid = false
					continueCheck=false
				}
			}
			
			if(continueCheck){
				for(int i=0;i<=d.size()-1;i++){
					if(!r.contains(d[i])){
						valid=false
						continueCheck=false
						break
					}
				}
			}
			
			if(continueCheck){
				for(int i=0;i<=y.size()-1;i++){
					if(!r.contains(y[i])){
						valid = false
						break
					}
				}
			}
		}
		else
			valid=false
		//if there's no value return true
		if(!dateStr)
			valid = true
			
		return valid
	}
}